<template>
  <el-dialog :visible.sync="visible" width="70%" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 接口api编码 -->
          <el-form-item label="接口编码" prop="apiCode">
            <!--文本框-->
            {{dataForm.apiName}}--({{dataForm.apiCode}})
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 业务主键 主要用于获取数据 -->
          <el-form-item label="业务主键" prop="bizId">
            <!--文本框-->
            <el-input v-model="dataForm.bizId" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 业务主键 主要用于获取数据 -->
          <el-form-item label="创建时间" prop="createDate">
            <!--文本框-->
            <el-input v-model="dataForm.createDate" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口发送状态 0-未发送 1-发送中 2-发送成功 3-发送失败 -->
          <el-form-item label="发送状态" prop="sendStatus">
            <!--文本框-->
            <el-select v-model="dataForm.sendStatus" disabled>
              <el-option v-for="template in $getDictList('API_SEND_STATUS', true)"
                         :key="template.val"
                         :label="template.label"
                         :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口发送时间 -->
          <el-form-item label="发送时间" prop="sendTime">
            <!--日期框-->
            <el-input v-model="dataForm.sendTime" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口响应状态 -->
          <el-form-item label="响应状态" prop="resultStatus">
            <!--文本框-->
            <el-input v-model="dataForm.resultStatus" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口发送状态描述  做截取 详细看发送接口日志 -->
          <el-form-item label="发送描述" prop="sendDesc">
            <!--文本框-->
            <el-input v-model="dataForm.sendDesc" type="textarea" :rows="2" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口响应数据 -->
          <el-form-item label="响应数据" prop="resultData">
            <!--文本框-->
            <el-input v-model="dataForm.resultData" type="textarea" :rows="5" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 回写状态 0-未回写 1-回写中 2-回写成功 3-回写失败 -->
          <el-form-item label="回写状态" prop="feedbackStatus">
            <!--文本框-->
            <el-select v-model="dataForm.feedbackStatus" disabled>
              <el-option v-for="template in $getDictList('API_FEEDBACK_STATUS', true)"
                         :key="template.val"
                         :label="template.label"
                         :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 回写时间 -->
          <el-form-item label="回写时间" prop="feedbackTime">
            <!--日期框-->
            <el-input v-model="dataForm.feedbackTime" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 回写尝试次数 -->
          <el-form-item label="回写尝试次数"
                        prop="feedbackRetry">
            <!--文本框-->
            <el-input v-model="dataForm.feedbackRetry" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 回写状态描述 -->
          <el-form-item label="回写状态描述" prop="feedbackDesc">
            <!--文本框-->
            <el-input v-model="dataForm.feedbackDesc" type="textarea" :rows="3" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('close') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        interfaceNo: '',
        bizId: '',
        sendStatus: '',
        sendTime: '',
        sendDesc: '',
        resultStatus: '',
        resultData: '',
        feedbackStatus: '',
        feedbackTime: '',
        feedbackDesc: '',
        feedbackRetry: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {}
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/api/apiNotifyInfo/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/apiNotifyInfo/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
